package com.acce.core.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

public class BaseController {
	
		protected Logger logger = Logger.getLogger(this.getClass());
		
		public String sendhttp(String serviceAddr, Map<String, String> params){
			String returnString = "";
			logger.info("wape-web::sendhttp::addr::" + serviceAddr);
			// 创建默认的httpClient实例.    
	        CloseableHttpClient httpclient = HttpClients.createDefault();  
	        try{
	        	// 创建httppost  
		        HttpPost httppost = new HttpPost(serviceAddr);
		        List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
		        Iterator<String> it = params.keySet().iterator();
		        while(it.hasNext()){
		        	String key = it.next();
		        	String value = params.get(key);
		        	formparams.add(new BasicNameValuePair(key,value));
		        }
		        logger.info("wape-web::sendhttp::params::" + formparams.toString());
		        UrlEncodedFormEntity uefEntitygs = new UrlEncodedFormEntity(formparams,"UTF-8");
		        httppost.setEntity(uefEntitygs); 
		        CloseableHttpResponse response = httpclient.execute(httppost);  //发送post请求并响应的内容
				try {
					HttpEntity entity = response.getEntity(); 
		            returnString=EntityUtils.toString(entity, "UTF-8");
		            logger.info("wape-web::sendhttp::return content::" + returnString);
				} catch (Exception e) {
					logger.info("wape-web::sendhttp::" + e.getMessage());
				}finally{
					response.close();
				}
	        }catch(Exception e){
	        	e.printStackTrace();
	        }finally{
	        	 // 关闭连接,释放资源    
	            try {  
	                httpclient.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }
	        return returnString;
		}
		
		
		public void exceptionWriteReturn(String serviceCode, String returnCode, String returnMsg, String cacheTime, String checkTime,HttpServletResponse response){
			JSONObject json = new JSONObject();
			json.put("serviceCode", serviceCode);
			json.put("returnCode", returnCode);
			json.put("returnMsg", returnMsg);
			json.put("cacheTime", cacheTime);
			json.put("checkTime", checkTime);
			writeJsonToResponse(response, json.toJSONString());
		}

		/**
		 * 向客户端输出
		 * @param response
		 * @param json
		 */
		public void writeJsonToResponse(HttpServletResponse response,String json) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.println(json);
				writer.flush();
			} catch (IOException e) {
				logger.error("wape-web::writeJsonToResponse::输出异常"+json,e);
			} finally {
				if(null != writer) {
					writer.close();
				}
			}
		}
}
